package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.entity.OrderDO;
import com.ssy.lingxi.order.entity.OrderRequirementDO;
import com.ssy.lingxi.order.model.bo.OrderRequirementDetailBO;
import com.ssy.lingxi.order.model.vo.basic.request.OrderRequirementVO;
import com.ssy.lingxi.order.model.vo.basic.response.OrderRequirementDetailVO;
import com.ssy.lingxi.order.repository.OrderRequirementRepository;
import com.ssy.lingxi.order.service.base.IBaseOrderRequirementService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 订单其他要求相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-19
 */
@Service
public class BaseOrderRequirementServiceImpl implements IBaseOrderRequirementService {
    @Resource
    private OrderRequirementRepository orderRequirementRepository;

    /**
     * 检查并生成订单其他要求，调用方要保存OrderDO
     *
     * @param order    订单
     * @param remark   订单备注
     * @param isCreate 是否新增，true-新增，false-修改
     * @return 检查结果
     */
    @Override
    public Wrapper<Void> checkOrderRemark(OrderDO order, String remark, boolean isCreate) {
        if(!isCreate) {
            orderRequirementRepository.deleteByOrder(order);
        }

        if(!StringUtils.hasText(remark)) {
            order.setRequirement(null);
            return Wrapper.success();
        }

        OrderRequirementDO requirement = new OrderRequirementDO();
        requirement.setOrder(order);
        requirement.setDetail(new OrderRequirementDetailBO("", remark));

        order.setRequirement(requirement);

        return Wrapper.success();
    }

    /**
     * 检查并生成订单其他要求，调用方要保存OrderDO
     *
     * @param order         订单
     * @param requirement   其他要求接口参数
     * @param isCreate      是否新增，true-新增，false-修改
     * @return 检查结果
     */
    @Override
    public Wrapper<Void> checkOrderRequirement(OrderDO order, OrderRequirementVO requirement, boolean isCreate) {
        if(!isCreate) {
            orderRequirementRepository.deleteByOrder(order);
        }

        if(requirement == null) {
            order.setRequirement(null);
            return Wrapper.success();
        }

        OrderRequirementDO orderRequirement = new OrderRequirementDO();
        orderRequirement.setOrder(order);
        orderRequirement.setDetail(new OrderRequirementDetailBO(requirement.getPack(), requirement.getRemark()));

        order.setRequirement(orderRequirement);

        return Wrapper.success();
    }

    /**
     * （转单） - 检查并生成订单其他要求，调用方要保存OrderDO
     *
     * @param order         转单后的订单
     * @param separateOrder 原订单
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> transferOrderRequirement(OrderDO order, OrderDO separateOrder) {
        OrderRequirementDO separateRequirement = orderRequirementRepository.findFirstByOrder(separateOrder);
        if(separateRequirement == null) {
            order.setRequirement(null);
        } else {
            OrderRequirementDO requirement = new OrderRequirementDO();
            requirement.setOrder(order);
            requirement.setDetail(separateRequirement.getDetail());

            order.setRequirement(requirement);
        }

        return Wrapper.success();
    }

    /**
     * 查询订单其他要求
     *
     * @param order 订单
     * @return 订单其他要求
     */
    @Override
    public OrderRequirementDetailVO getOrderRequirement(OrderDO order) {
        OrderRequirementDetailVO detailVO = new OrderRequirementDetailVO();
        OrderRequirementDO requirement = orderRequirementRepository.findFirstByOrder(order);
        if(requirement == null || requirement.getDetail() == null) {
            return detailVO;
        }

        detailVO.setPack(StringUtils.hasLength(requirement.getDetail().getPack()) ? requirement.getDetail().getPack() : "");
        detailVO.setRemark(StringUtils.hasLength(requirement.getDetail().getRemark()) ? requirement.getDetail().getRemark() : "");
        return detailVO;
    }
}
